package com.qst.crop.controller;

/**
 * @author YGD
 * @日期： 2025-07-07-09-11
 * @habit: like girls
 **/

import com.qst.crop.common.Result;
import com.qst.crop.entity.Address;
import com.qst.crop.service.AddressService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

@RestController
@RequestMapping({"/address"})
@Api(
        tags = {"地址模块接口"}
)
public class AddressController {
    @Autowired
    private AddressService addressService;

    public AddressController() {
    }

    @ApiOperation("添加地址 ")
    @PostMapping({"/add"})
    public Result add(@RequestBody Address address, BindingResult bindingResult) {
        if (!bindingResult.hasErrors()) {
            this.addressService.add(address);
            return new Result(true, 20000, "添加成功");
        } else {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            Iterator var5 = allErrors.iterator();

            while(var5.hasNext()) {
                ObjectError objectError = (ObjectError)var5.next();
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }

            String s = stringBuffer.toString();
            return new Result(false, 20001, "添加失败", s);
        }
    }

    @ApiOperation("根据登录用户名查询所有地址")
    @GetMapping({"/selectByOwnName"})
    public Result<List<Address>> selectByOwnName() {
        List<Address> addresses = this.addressService.selectByOwnName();
        return new Result(true, 20000, "查询成功", addresses);
    }

    @ApiOperation("根据登录用户名查询默认地址")
    @GetMapping({"/selectDefaultByOwnName"})
    public Result<Address> selectDefaultByOwnName() {
        Address address = this.addressService.selectDefaultByOwnName();
        if (null == address) {
            List<Address> addresses = this.addressService.selectByOwnName();
            if (null != addresses && addresses.size() > 0) {
                address = (Address)addresses.get(0);
            }
        }

        return new Result(true, 20000, "查询成功", address);
    }

    @ApiOperation("默认地址信息更新操作")
    @PostMapping({"/defaultAddressInfoUpdate"})
    public Result<String> defaultAddressInfoUpdate(@Validated @RequestBody Address address, BindingResult bindingResult) {
        if (!bindingResult.hasErrors()) {
            this.addressService.defaultAddressInfoUpdate(address);
            return new Result(true, 20000, "修改成功");
        } else {
            StringBuffer stringBuffer = new StringBuffer();
            List<ObjectError> allErrors = bindingResult.getAllErrors();
            Iterator var5 = allErrors.iterator();

            while(var5.hasNext()) {
                ObjectError objectError = (ObjectError)var5.next();
                stringBuffer.append(objectError.getDefaultMessage()).append("; ");
            }

            String s = stringBuffer.toString();
            return new Result(false, 20001, "地址信息更新失败", s);
        }
    }

    @ApiOperation("根据id修改地址")
    @PostMapping({"/update/{id}"})
    public Result update(@PathVariable("id") Integer id, @RequestBody Address address) {
        address.setId(id);
        this.addressService.update(address);
        return new Result(true, 20000, "修改成功");
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping({"/delete/{id}"})
    public Result delete(@PathVariable("id") Integer id) {
        boolean delete = this.addressService.delete(id);
        return !delete ? new Result(false, 20001, "该地址为默认地址不能删除") : new Result(true, 20000, "删除成功");
    }
}

